<template>
  <div class="visitor-preregistration">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">{{ "观众预登记" }}</h1>
        <p class="page-subtitle">
          {{ pageData.subtitle || "免费预登记，享受更多参观便利" }}
        </p>
      </div>
    </div>

    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('group-visit')"
            :class="['nav-item', { active: activeTab === 'group-visit' }]"
          >
            组团参观
          </a>
          <a
            @click="setActiveTab('visitor-preregistration')"
            :class="[
              'nav-item',
              { active: activeTab === 'visitor-preregistration' },
            ]"
          >
            观众预登记
          </a>
          <a
            @click="setActiveTab('visitor-notice')"
            :class="['nav-item', { active: activeTab === 'visitor-notice' }]"
          >
            观众须知
          </a>
          <a
            @click="setActiveTab('visa-system')"
            :class="['nav-item', { active: activeTab === 'visa-system' }]"
          >
            签证申请
          </a>
          <a
            @click="setActiveTab('visitor-roster')"
            :class="['nav-item', { active: activeTab === 'visitor-roster' }]"
          >
            展商名录
          </a>
          <a
            @click="setActiveTab('visitor-reviews')"
            :class="['nav-item', { active: activeTab === 'visitor-reviews' }]"
          >
            观众评语
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <!-- 预登记优势 -->
      <section class="benefits-section">
        <!-- <h2 class="section-title">预登记优势</h2> -->
        <div class="benefits-grid">
          <div
            class="benefit-item"
            v-for="benefit in benefits"
            :key="benefit.id"
          >
            <div class="benefit-icon">
              <el-icon><component :is="benefit.icon" /></el-icon>
            </div>
            <h3>{{ benefit.title }}</h3>
            <p>{{ benefit.description }}</p>
          </div>
        </div>
      </section>

      <!-- 预登记流程 -->
      <section class="process-section">
        <h2 class="section-title">预登记流程</h2>
        <div class="process-steps">
          <div
            class="step-item"
            v-for="(step, index) in processSteps"
            :key="step.id"
          >
            <div class="step-number">{{ index + 1 }}</div>
            <div class="step-content">
              <h3>{{ step.title }}</h3>
              <p>{{ step.description }}</p>
            </div>
            <div class="step-arrow" v-if="index < processSteps.length - 1">
              <el-icon><ArrowRight /></el-icon>
            </div>
          </div>
        </div>
      </section>

      <!-- 预登记表单 -->
      <section class="form-section">
        <h2 class="section-title">在线预登记</h2>
        <div class="form-container">
          <el-form
            ref="preregistrationForm"
            :model="formData"
            :rules="formRules"
            label-width="80px"
            class="preregistration-form"
          >
            <!-- 基本信息 -->
            <div class="form-group">
              <h3 class="group-title">基本信息</h3>
              <div class="form-item">
                <el-form-item label="姓名" prop="name">
                  <el-input
                    v-model="formData.name"
                    placeholder="请输入真实姓名"
                  />
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                  <el-select
                    v-model="formData.gender"
                    placeholder="请选择性别"
                    style="width: 100%"
                  >
                    <el-option label="男" value="male" />
                    <el-option label="女" value="female" />
                  </el-select>
                </el-form-item>
              </div>
              <div class="form-item">
                <el-form-item label="手机号码" prop="phone">
                  <el-input
                    v-model="formData.phone"
                    placeholder="请输入手机号码"
                  />
                </el-form-item>
                <el-form-item label="邮箱地址" prop="email">
                  <el-input
                    v-model="formData.email"
                    placeholder="请输入邮箱地址"
                  />
                </el-form-item>
              </div>
              <!-- <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="姓名" prop="name">
                    <el-input
                      v-model="formData.name"
                      placeholder="请输入真实姓名"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="性别" prop="gender">
                    <el-select
                      v-model="formData.gender"
                      placeholder="请选择性别"
                      style="width: 100%"
                    >
                      <el-option label="男" value="male" />
                      <el-option label="女" value="female" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row> -->

              <!-- <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="手机号码" prop="phone">
                    <el-input
                      v-model="formData.phone"
                      placeholder="请输入手机号码"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="邮箱地址" prop="email">
                    <el-input
                      v-model="formData.email"
                      placeholder="请输入邮箱地址"
                    />
                  </el-form-item>
                </el-col>
              </el-row> -->
            </div>

            <!-- 单位信息 -->
            <div class="form-group">
              <h3 class="group-title">单位信息</h3>
              <div class="form-item">
                <el-form-item label="公司名称" prop="company">
                  <el-input
                    v-model="formData.company"
                    placeholder="请输入公司名称"
                  />
                </el-form-item>
                <el-form-item label="职位" prop="position">
                  <el-input
                    v-model="formData.position"
                    placeholder="请输入职位"
                  />
                </el-form-item>
              </div>
              <div class="form-item">
                <el-form-item label="行业类型" prop="industry">
                  <el-select
                    v-model="formData.industry"
                    placeholder="请选择行业类型"
                    style="width: 100%"
                  >
                    <el-option label="制造业" value="制造业" />
                    <el-option label="农业" value="农业" />
                    <el-option label="建筑" value="建筑" />
                    <el-option label="能源" value="能源" />
                    <el-option label="商贸物流" value="商贸物流" />
                    <el-option label="医疗健康" value="医疗健康" />
                    <el-option label="养老服务" value="养老服务" />
                    <el-option label="教育" value="教育" />
                    <el-option label="商业社区服务" value="商业社区服务" />
                    <el-option
                      label="安全应急和极限环境作业"
                      value="安全应急和极限环境作业"
                    />
                    <el-option label="其他" value="其他" />
                  </el-select>
                </el-form-item>
                <el-form-item label="公司规模" prop="companySize">
                  <el-select
                    v-model="formData.companySize"
                    placeholder="请选择公司规模"
                    style="width: 100%"
                  >
                    <el-option label="1-50人" value="small" />
                    <el-option label="51-200人" value="medium" />
                    <el-option label="201-1000人" value="large" />
                    <el-option label="1000人以上" value="enterprise" />
                  </el-select>
                </el-form-item>
              </div>
            </div>

            <!-- 参观信息 -->
            <div class="form-group">
              <h3 class="group-title">参观信息</h3>
              <div class="form-item">
                <el-form-item label="参观日期" prop="visitDate">
                  <el-date-picker
                    v-model="formData.visitDate"
                    type="date"
                    placeholder="请选择参观日期"
                    style="width: 100%"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
                <el-form-item label="关注产品" prop="interestedProducts">
                  <el-select
                    v-model="formData.interestedProducts"
                    placeholder="请选择关注的产品"
                    style="width: 100%"
                  >
                    <el-option
                      label="机器人核心零部件（含成熟和创新产品）"
                      value="机器人核心零部件（含成熟和创新产品）"
                    />
                    <el-option
                      label="大脑相关（人工智能算法及其他）"
                      value="大脑相关（人工智能算法及其他）"
                    />
                    <el-option
                      label="整机研发、设计、系统集成"
                      value="整机研发、设计、系统集成"
                    />
                    <el-option
                      label="机器人检测、维修和保养、测试验证等"
                      value="机器人检测、维修和保养、测试验证等"
                    />
                    <el-option
                      label="机器人生产设备、流水线、代工厂等"
                      value="机器人生产设备、流水线、代工厂等"
                    />
                    <el-option label="其他，请说明" value="其他" />
                  </el-select>
                </el-form-item>
              </div>

              <el-form-item label="参观目的" prop="visitPurpose">
                <el-checkbox-group v-model="formData.visitPurpose">
                  <el-checkbox label="technology">了解新技术</el-checkbox>
                  <el-checkbox label="cooperation">商务合作</el-checkbox>
                  <el-checkbox label="supplier">寻找供应商</el-checkbox>
                  <el-checkbox label="research">市场调研</el-checkbox>
                  <el-checkbox label="learning">学习交流</el-checkbox>
                </el-checkbox-group>
              </el-form-item>

              <el-form-item label="特殊需求" prop="specialRequirements">
                <el-input
                  v-model="formData.specialRequirements"
                  type="textarea"
                  :rows="3"
                  placeholder="如需要专业导览、无障碍设施等，请说明"
                />
              </el-form-item>
            </div>

            <!-- 提交按钮 -->
            <div class="form-actions">
              <el-checkbox v-model="formData.agree">
                我已阅读并同意
                <router-link to="/sys-privacy?type=visitorTerms">
                  《参观条款》
                </router-link>
                和
                <router-link to="/sys-privacy?type=visitorPrivacy">
                  《隐私政策》
                </router-link>
              </el-checkbox>
              <div class="btn-box">
                <el-button
                  type="primary"
                  size="large"
                  @click="handleSubmit"
                  :loading="loading"
                >
                  提交预登记
                </el-button>
                <el-button size="large" @click="resetForm">
                  重置表单
                </el-button>
              </div>
            </div>
          </el-form>
        </div>
      </section>

      <!-- 重要提示 -->
      <section class="notice-section">
        <h2 class="section-title">重要提示</h2>
        <div class="notice-content">
          <el-alert
            title="预登记截止时间"
            description="观众预登记将于2025年3月8日24:00截止，请尽早完成预登记。"
            type="warning"
            :closable="false"
            show-icon
          />
          <div class="notice-list">
            <h3>注意事项：</h3>
            <ul>
              <li>预登记成功后，请妥善保存确认邮件，现场换证时需要出示</li>
              <li>现场换证时请携带有效身份证件（身份证、护照等）</li>
              <li>展会期间请遵守现场管理规定，配合工作人员安排</li>
              <li>如需修改预登记信息，请在截止时间前联系客服</li>
              <li>团体参观（20人以上）请联系组委会安排专业导览服务</li>
            </ul>
          </div>
          <div class="contact-info">
            <h3>联系我们：</h3>
            <p>客服热线：400-123-4567</p>
            <p>客服邮箱：visitor@fairplus.com</p>
            <p>工作时间：周一至周五 9:00-18:00</p>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { visitorApi, subpageApi } from "@/api/user";
import {
  ArrowRight,
  Clock,
  Document,
  Check,
  Star,
} from "@element-plus/icons-vue";

export default {
  name: "VisitorPreregistration",
  components: {
    ArrowRight,
    Clock,
    Document,
    Check,
    Star,
  },
  setup() {
    const activeTab = ref("visitor-preregistration");
    const bannerImage = ref("");
    const loadBanner = async () => {
      try {
        const res = await subpageApi.getContentByType("carousel_visitors");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };
    const preregistrationForm = ref();
    const loading = ref(false);
    const pageData = reactive({
      title: "",
      subtitle: "",
    });

    // 预登记优势
    const benefits = ref([
      {
        id: 1,
        icon: "Clock",
        title: "免费快速入场",
        description: "避免现场排队，快速换证入场，节省宝贵时间",
      },
      {
        id: 2,
        icon: "Document",
        title: "专业资料包",
        description: "提前获得展商名录、展会指南等专业资料",
      },
      {
        id: 3,
        icon: "Check",
        title: "优先参加活动",
        description: "优先报名参加论坛、技术交流会等同期活动",
      },
      {
        id: 4,
        icon: "Star",
        title: "专业服务",
        description: "享受专业导览、商务对接等增值服务",
      },
    ]);

    // 预登记流程
    const processSteps = ref([
      {
        id: 1,
        title: "填写信息",
        description: "完整填写预登记表单信息",
      },
      {
        id: 2,
        title: "提交审核",
        description: "提交表单等待组委会审核",
      },
      {
        id: 3,
        title: "确认通知",
        description: "收到预登记成功确认邮件",
      },
      {
        id: 4,
        title: "现场换证",
        description: "携带身份证件现场快速换证",
      },
    ]);

    // 表单数据
    const formData = reactive({
      name: "",
      gender: "",
      phone: "",
      email: "",
      company: "",
      position: "",
      industry: "",
      companySize: "",
      visitDate: "",
      interestedProducts: "",
      visitPurpose: [],
      specialRequirements: "",
      agree: false,
    });

    // 表单验证规则
    const formRules = {
      name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
      phone: [
        { required: true, message: "请输入手机号码", trigger: "blur" },
        {
          pattern: /^1[3-9]\d{9}$/,
          message: "请输入正确的手机号码",
          trigger: "blur",
        },
      ],
      email: [
        { required: true, message: "请输入邮箱地址", trigger: "blur" },
        { type: "email", message: "请输入正确的邮箱格式", trigger: "blur" },
      ],
      company: [{ required: true, message: "请输入公司名称", trigger: "blur" }],
      visitDate: [
        { required: true, message: "请选择参观日期", trigger: "change" },
      ],
    };

    // 提交表单
    const handleSubmit = async () => {
      if (!formData.agree) {
        ElMessage.warning("请先阅读并同意参观条款和隐私政策");
        return;
      }

      try {
        await preregistrationForm.value.validate();
        loading.value = true;

        const payload = {
          name: formData.name,
          gender: formData.gender,
          phone: formData.phone,
          email: formData.email,
          company: formData.company,
          position: formData.position,
          industry: formData.industry,
          companySize: formData.companySize,
          visitDate: formData.visitDate,
          interestedProducts: formData.interestedProducts,
          visitPurpose: formData.visitPurpose,
          specialRequirements: formData.specialRequirements,
        };
        const response = await visitorApi.preRegister(payload);

        if (response && (response.code === "200" || response.code === 200)) {
          ElMessage.success("预登记提交成功！确认邮件将发送至您的邮箱");
          resetForm();
        } else {
          ElMessage.error(response?.msg || "预登记提交失败，请重试");
        }
      } catch (error) {
        console.error("预登记失败:", error);
        ElMessage.error(error.message || "预登记提交失败，请重试");
      } finally {
        loading.value = false;
      }
    };

    // 重置表单
    const resetForm = () => {
      if (preregistrationForm.value) {
        preregistrationForm.value.resetFields();
      }
      formData.interestedProducts = "";
      formData.visitPurpose = [];
      formData.agree = false;
    };

    // 显示条款
    const showTerms = () => {
      ElMessage.info("参观条款页面");
    };

    // 显示隐私政策
    const showPrivacy = () => {
      ElMessage.info("隐私政策页面");
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "visitor-preregistration") {
        window.location.href = `/${tabKey}`;
      }
    };

    onMounted(loadBanner);

    return {
      activeTab,
      bannerImage,
      preregistrationForm,
      loading,
      pageData,
      benefits,
      processSteps,
      formData,
      formRules,
      handleSubmit,
      resetForm,
      showTerms,
      showPrivacy,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.visitor-preregistration {
  min-height: 100vh;
  background: linear-gradient(
    199deg,
    var(--e-global-color-a3699a4) 0%,
    var(--e-global-color-5c1ca3d) 100%
  );
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.section-title {
  font-size: 2rem;
  color: white;
  text-align: center;
  margin-bottom: 40px;
  font-weight: 600;
}

/* 预登记优势样式 */
.benefits-section {
  padding: 80px 0;
}

.benefits-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
}

.benefit-item {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 30px;
  text-align: center;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.benefit-item:hover {
  transform: translateY(-10px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
}

.benefit-icon {
  font-size: 3rem;
  color: #fff;
  margin-bottom: 20px;
}

.benefit-item h3 {
  font-size: 1.3rem;
  color: white;
  margin-bottom: 15px;
  font-weight: 600;
}

.benefit-item p {
  color: rgba(255, 255, 255, 0.8);
  line-height: 1.6;
  margin: 0;
}

/* 流程步骤样式 */
.process-section {
  /* padding: 80px 0; */
}

.process-steps {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* max-width: 1000px; */
  margin: 0 auto;
}

.step-item {
  display: flex;
  gap: 18px;
  align-items: center;
  flex: 1;
}

.step-number {
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, #ff6b6b, #ffa726);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  font-weight: bold;
  color: white;
}
.step-content {
  flex: 1;
}
.step-content h3 {
  color: white;
  font-size: 1.1rem;
  margin-bottom: 5px;
}

.step-content p {
  color: rgba(255, 255, 255, 0.8);
  margin: 0;
  font-size: 0.9rem;
}

.step-arrow {
  color: white;
  font-size: 1.5rem;
  margin: 0 20px;
}

/* 表单样式 */
.form-section {
  padding: 80px 0;
}

.form-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
}

.preregistration-form {
  max-width: none;
}

.form-group {
  margin-bottom: 40px;
}

.group-title {
  font-size: 1.3rem;
  color: #2c3e50;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 2px solid #e1e8ed;
  font-weight: 600;
}

.form-item {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
  @media (max-width: 768px) {
    grid-template-columns: 1fr;
  }
}

.form-actions {
  margin-top: 40px;
  /* text-align: center; */
}

.btn-box {
  text-align: center;
  margin-top: 20px;
}

.btn-box .el-button {
  /* padding: 12px 30px; */
  font-size: 1rem;
}

/* 注意事项样式 */
.notice-section {
  /* padding-top: 20px; */
  padding-bottom: 80px;
}

.notice-content {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.1);
}

.notice-list {
  margin: 30px 0;
}

.notice-list h3 {
  color: #2c3e50;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.notice-list ul {
  list-style: none;
  padding: 0;
}

.notice-list li {
  padding: 8px 0;
  padding-left: 20px;
  position: relative;
  color: #555;
  line-height: 1.6;
}

.notice-list li::before {
  content: "•";
  color: #320063;
  font-weight: bold;
  position: absolute;
  left: 0;
}

.contact-info {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e1e8ed;
}

.contact-info h3 {
  color: #2c3e50;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.contact-info p {
  margin: 5px 0;
  color: #555;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .benefits-grid {
    grid-template-columns: 1fr;
  }

  .process-steps {
    flex-direction: column;
    gap: 30px;
  }

  .step-arrow {
    transform: rotate(90deg);
    margin: 10px 0;
  }

  .form-container {
    padding: 20px;
  }

  .notice-content {
    padding: 20px;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
